package com.xywl.escort.view.order.repo

import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.text.Spannable
import android.text.SpannableString
import android.text.TextUtils
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.trello.rxlifecycle2.LifecycleTransformer
import com.xywl.escort.R
import com.xywl.escort.base.BaseActivity
import com.xywl.escort.bean.BaseResp
import com.xywl.escort.bean.RepoOrderResp
import com.xywl.escort.utils.NumberFormatUtils
import com.xywl.escort.utils.ToastUtil
import com.xywl.escort.widget.GlideApp
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_repo_detail.*
import javax.inject.Inject

/**
 * 收购详情
 */
@Route(path = "/xy/order/repo/detail")
class RepoDetailActivity : BaseActivity(), RepoDetailContract.View {
    @Autowired
    @JvmField
    var dataBean: RepoOrderResp.DataBean.ListBean? = null

    @Inject
    lateinit var mPresenter: RepoDetailContract.Presenter

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_repo_detail)
        ARouter.getInstance().inject(this)
        initView()

        mPresenter.takeView(this)
        addListener()
    }

    private fun initView() {
        showBackButton()
        setTitleText("订单详情")

        GlideApp.with(this)
                .load(dataBean!!.coverUrl)
                .centerCrop()
                .placeholder(R.mipmap.icon_default_picture)
                .into(img_project)

        val status = dataBean!!.auditStatus
        // 收购审核状态 1待审核，2审核通过，3审核不通过
        when (status) {
            1 -> {
                text_status.text = "申请收购"
                btn_delete_order.visibility = View.GONE
            }
            2 -> {
                text_status.text = "收购成功"
                btn_delete_order.visibility = View.VISIBLE
            }
            3 -> {
                text_status.text = "收购失败"
                btn_delete_order.visibility = View.VISIBLE
            }
            else -> {
                text_status.text = "取消收购"
                btn_delete_order.visibility = View.VISIBLE
            }
        }

        if (null != dataBean!!.marketPriceYuan) {
            val marketPrice = "市场价/份 " + NumberFormatUtils.getInstance().format(dataBean!!.marketPriceYuan.toDouble()) + "元"
            text_market_price.text = setText(marketPrice)
        }
        if (null != dataBean!!.subscribePriceYuan) {
            val subscriptionPrice = "认购价/份 " + NumberFormatUtils.getInstance().format(dataBean!!.subscribePriceYuan.toDouble()) + "元"
            text_subscription_price.text = setText(subscriptionPrice)
        }
        text_first_line.text = "分红"
        circleProgress.setMax(100)
        circleProgress.setProgress(100)
        text_title.text = dataBean!!.projectName

        val repoNum = "收购份数:" + dataBean!!.num + "份"
        text_repo_num.text = repoNum

        if (null != dataBean!!.priceYuan) {
            val repoPrice = "收购价格:" + NumberFormatUtils.getInstance().format(dataBean!!.priceYuan.toDouble()) + "元"
            text_repo_price.text = repoPrice
        }

        if (null != dataBean!!.totalPriceYuan) {
            val totalRepoPrice = "收购总价:" + NumberFormatUtils.getInstance().format(dataBean!!.totalPriceYuan.toDouble()) + "元"
            text_repo_total_price.text = totalRepoPrice
        }

        if (!TextUtils.isEmpty(dataBean!!.remark)) {
            val remark = "备注:" + dataBean!!.remark
            text_remark.visibility = View.VISIBLE
            text_remark.text = remark
        } else {
            text_remark.visibility = View.GONE
        }
    }

    private fun addListener() {
        btn_delete_order.setOnClickListener {
            mPresenter.delOrder(dataBean!!.id)
        }
    }

    private fun setText(text: String): SpannableString {
        val spanString = SpannableString(text)
        //再构造一个改变字体颜色的Span
        val span = ForegroundColorSpan(ContextCompat.getColor(this, R.color.aid_prompt_color))
        //将这个Span应用于指定范围的字体
        spanString.setSpan(span, text.indexOf("份") + 1, text.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        val span1 = AbsoluteSizeSpan(15, true)
        spanString.setSpan(span1, text.indexOf("份") + 1, text.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        return spanString
    }

    override fun isActive(): Boolean {
        return false
    }

    override fun <T : Any?> bindToLife(): LifecycleTransformer<T> {
        return this.bindToLifecycle()
    }

    override fun delOrderSuccess(resp: BaseResp?) {
        ToastUtil.showTip(this, "删除订单成功！！！")
        finish()
    }

    override fun delOrderFailure(msg: String?) {
        ToastUtil.showTip(this, msg)
    }
}
